package admin

import (
	"encoding/json"
	"ginadmin/app/common"
	"ginadmin/app/dao"
	"ginadmin/app/model"
	"net/http"
	"strconv"
	"strings"

	"github.com/gin-gonic/gin"
)

type RoleController struct {
	BaseController
}

// 列表页面
func (con RoleController) Index(c *gin.Context) {

	c.HTML(http.StatusOK, "role/index.html", nil)
}

// 添加/编辑页面
func (con RoleController) PostPage(c *gin.Context) {

	idstr := c.DefaultQuery("id", "0")

	id, _ := strconv.Atoi(idstr)

	if id == 1 {
		con.Error(c, "无法编辑超级管理员!")
		return
	}

	var role model.AdminRole

	if id > 0 {
		role.ID = uint(id)
		dao.DB.First(&role)
	}

	c.HTML(http.StatusOK, "role/edit.html", gin.H{
		"id":   id,
		"info": role,
	})
}

type RoleForm struct {
	Name  string              `json:"name"`
	Id    uint                `json:"id"`
	Rules []map[string]string `json:"rules"`
}

func (con RoleController) Post(c *gin.Context) {

	var roleForm RoleForm

	json.NewDecoder(c.Request.Body).Decode(&roleForm)

	if roleForm.Id == 1 {
		con.Error(c, "无法编辑超级管理员!")
		return
	}

	var role model.AdminRole

	role.ID = roleForm.Id
	role.Name = roleForm.Name

	rulestr := ""
	for _, v := range roleForm.Rules {
		rulestr += v["nodeId"] + ","
	}

	rulestr = strings.TrimRight(rulestr, ",")

	role.Rules = rulestr

	if role.ID > 0 {

		dao.DB.Updates(&role)

	} else {

		dao.DB.Create(&role)
	}

	con.Success(c, "操作成功")
}

// 删除
func (con RoleController) Delete(c *gin.Context) {
	idstr := c.DefaultQuery("id", "0")

	id, _ := strconv.Atoi(idstr)

	if id == 1 {
		con.Error(c, "无法删除超级管理员!")
		return
	}

	role := model.AdminRole{}

	role.ID = uint(id)

	dao.DB.Delete(&role)

	con.Success(c, "操作成功")
}

// 获取列表
func (con RoleController) GetList(c *gin.Context) {

	var list []model.AdminRole

	var count int64 = 0

	strpage := c.DefaultQuery("page", "1")
	strlimit := c.DefaultQuery("limit", strconv.FormatInt(int64(common.LIMIT), 10))
	limit, _ := strconv.Atoi(strlimit)
	page, _ := strconv.Atoi(strpage)

	name := c.DefaultQuery("name", "")

	dao.DB.Where("name like ?", name+"%").Limit(limit).Offset((page - 1) * limit).Order("id ASC").Find(&list)
	dao.DB.Where("name like ?", name+"%").Model(&model.AdminRole{}).Count(&count)

	con.Success(c, "ok", &list, count)
}

// 获取多选菜单列表
func (con RoleController) GetMenu(c *gin.Context) {

	var menuapi = model.AdminMenuApis{}

	dao.DB.Table("gin_admin_menu").Order("sort ASC").Scan(&menuapi)

	idstr := c.DefaultQuery("id", "0")
	id, _ := strconv.Atoi(idstr)
	if id > 0 {
		var role = model.AdminRole{
			BaseModel: model.BaseModel{ID: uint(id)},
		}

		dao.DB.First(&role)

		rules := strings.Split(role.Rules, ",")

		//查询当前角色已选择菜单
		var checkMenus []model.AdminMenu
		dao.DB.Where("id IN ? ", rules).Order("sort ASC").Find(&checkMenus)

		for k, v := range menuapi {

			menuapi[k].CheckArr = map[string]uint8{
				"checked": 0,
			}

			for _, c := range checkMenus {

				if v.ID == c.ID {

					menuapi[k].CheckArr = map[string]uint8{
						"checked": 1,
					}
					break
				}
			}
		}
	}

	treedata := menuapi.GetTree()

	var statusMap = make(map[string]interface{})

	statusMap["code"] = 200
	statusMap["message"] = "ok"

	c.JSON(http.StatusOK, gin.H{
		"status": statusMap,
		"data":   treedata,
	})

}
